package com.lj.mcore.service;

import com.lj.mcore.dao.base.BaseAppDAO;
import com.lj.mcore.entity.Goods;
import com.lj.mcore.entity.GoodsCart;
import com.lj.mcore.entity.GoodsClass;
import com.lj.mcore.entity.StoreCart;
import com.lj.mcore.tools.StoreCartTools;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class GoodsCartService implements IGoodsCartService {

    @Resource(name="goodsCartDao")
    private BaseAppDAO<GoodsCart> goodsCartDao;

    @Autowired
    IStoreCartService storeCartService;


    @Override
    public boolean save(GoodsCart obj) {
        return goodsCartDao.save(obj);
    }

    @Override
    public boolean saveWithStoreCart(GoodsCart obj) {
        Map query = new HashMap();
        query.put("deleteStatus",false);
        query.put("uid",obj.getUser().getId());
        query.put("sid",obj.getStore().getId());
        List<StoreCart> storeCarts = storeCartService.query(IStoreCartService.FIND_STORE_CART,query,0,1);
        if (storeCarts.size()<=0){
            StoreCart storeCart = new StoreCart();
            storeCart.setStore(obj.getStore());
            storeCart.setUser(obj.getUser());
            storeCart.setUpdateTime(new Date());
            // 第一次添加添加数据，计算总价
            double sell_price = obj.getPrice().doubleValue()*obj.getCount();
            storeCart.setTotal_price(new BigDecimal(sell_price));
            storeCartService.save(storeCart);
            obj.setStoreCart(storeCart);
            return goodsCartDao.save(obj);
        }else{
            obj.setStoreCart(storeCarts.get(0));
            goodsCartDao.save(obj);
            //保存后 再次查询一次，计算总价，防止遗漏。

            storeCarts = storeCartService.query(IStoreCartService.FIND_STORE_CART,query,0,1);
            StoreCart storeCart = storeCarts.get(0);
            List<GoodsCart> goodsCarts = storeCart.getGoodsCarts();
            //计算总价
            double total_price= StoreCartTools.count_storeCart_price(goodsCarts);
            storeCart.setTotal_price(new BigDecimal(total_price));
            storeCartService.update(storeCart);
            return true;
        }
    }

    @Override
    public boolean delete(Long id) {
        return false;
    }

    @Override
    public boolean update(GoodsCart obj) {
        return goodsCartDao.update(obj);
    }

    @Override
    public boolean UpdateWithStoreCart(GoodsCart obj) {
        goodsCartDao.update(obj);
        Map query = new HashMap();
        query.put("deleteStatus",false);
        query.put("uid",obj.getUser().getId());
        query.put("sid",obj.getStore().getId());
        List<StoreCart> storeCarts = storeCartService.query(IStoreCartService.FIND_STORE_CART,query,0,1);
        StoreCart storeCart = storeCarts.get(0);
        List<GoodsCart> goodsCarts = storeCart.getGoodsCarts();
        //计算总价
        double total_price= StoreCartTools.count_storeCart_price(goodsCarts);

        storeCart.setTotal_price(new BigDecimal(total_price));
        storeCartService.update(storeCart);
        return true;
    }

    @Override
    public GoodsCart getObjById(Long id) {
        return goodsCartDao.findById(id);
    }

    @Override
    public GoodsCart getSingleResult(Map<String, Object> map) {
        return goodsCartDao.getSingleResult(map);
    }

    @Override
    public List<GoodsCart> query(String paramString, Map paramMap, int paramInt1, int paramInt2) {
        return goodsCartDao.query(paramString, paramMap, paramInt1, paramInt2);
    }

    @Override
    public PageBean getList(int pageSize, int page, Map<String, String> query) {
        StringBuffer sb = new StringBuffer("from GoodsCart obj where obj.deleteStatus = false ");
        if(query != null && query.size() >0 ){

            if(!CommUtils.null2String(query.get("uid")).equals("")){
                sb.append(" and obj.user.id = '"+query.get("uid")+"' ");
            }

            if(!CommUtils.null2String(query.get("status")).equals("")){
                sb.append(" and obj.status = '"+query.get("status")+"' ");
            }
        }
        sb.append(" order by obj.addTime desc");

        String hql = sb.toString();

        List listNum = this.query("select count(obj.id) "+hql,null,0,1);
        int allRow=0;
        if (listNum!=null){
            allRow = CommUtils.null2Int(listNum.get(0));
        }

        pageSize=pageSize<=0?200:pageSize;
        pageSize=pageSize>200?200:pageSize;
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        page=page<=0?1:pageSize;
        page = page>totalPage?totalPage:page;
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List list = this.query(hql,null, offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
    }

    @Override
    public GoodsCart goodsIsExistInCard(long gid, long uid, Long gsid) {
        Map query = new HashMap();
        query.put("gid",gid);
        query.put("uid",uid);
        String hql = null;
        if (gsid==null){
            hql = "from GoodsCart obj where obj.deleteStatus = false and obj.user.id=:uid and obj.goods.id =:gid and obj.status =0 and obj.goodsStock.id is null";
        }else{
            query.put("gsid",gsid);
            hql = "from GoodsCart obj where obj.deleteStatus = false and obj.user.id=:uid and obj.goods.id =:gid and obj.status =0 and obj.goodsStock.id =:gsid";
        }
        List<GoodsCart> goodsCarts = goodsCartDao.query(hql,query,0,1);
        if (goodsCarts.size()==0){
            return null;
        }
        return goodsCarts.get(0);
    }
}
